﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cocos2d;
using Microsoft.Xna.Framework;

using CampaignWP7.Base;

namespace CampaignWP7
{
    class DynamicMgr:CCLayer
    {
        const int PoolVolum = 50;
        //
        static protected DynamicMgr instanceMgr = new DynamicMgr();
        public List<Bullet> m_BulletsPool;
        public List<Wolf> m_WolvesPool;
        public List<Boss> m_BossesPool;
        public List<EnemyBullet> m_EnemyBulletPool;
        public static DynamicMgr Instance() { return instanceMgr; }
        protected DynamicMgr()
        {
            m_BulletsPool = new List<Bullet>(PoolVolum);
            m_WolvesPool = new List<Wolf>(PoolVolum);
            m_BossesPool = new List<Boss>(PoolVolum);
            m_EnemyBulletPool = new List<EnemyBullet>(PoolVolum);
        }
        //
        public void Update(float dt)
        {
            for (int i = 0;i<m_BulletsPool.Count;++i)
            {
                Bullet bullet = m_BulletsPool[i];
                bullet.update(dt);
                if (!IsObjInScene(bullet))
                {
                    DelChild(bullet);
                }
            }

            for (int i = 0; i < m_EnemyBulletPool.Count; ++i)
            {
                EnemyBullet bullet = m_EnemyBulletPool[i];
                bullet.update(dt);
                if (!IsObjInScene(bullet))
                {
                    DelChild(bullet);
                }
            }
            //
            for (int i = 0; i < m_WolvesPool.Count; ++i)
            {
                Wolf wolf = m_WolvesPool[i];
                wolf.update(dt);
                if (!IsObjInScene(wolf))
                {
                    DelChild(wolf);
                }
                //
                if (wolf.CurrentState == Wolf.WolfState.WS_ALIVE)
                {
                    if (IsObjToBottom(wolf, GameLayerLayout.GoundHigh + wolf.height / 2))
                    {
                        wolf.stopAllActions();
                        wolf.PlayWalkAnimation();
                        wolf.PlayMoveToBuilding(5, new CCPoint(0, GameLayerLayout.GoundHigh + wolf.height / 2));
                    }
                }

            }

            for (int i = 0; i < m_BossesPool.Count; ++i)
            {
                Boss boss = m_BossesPool[i];
                if (boss.CurrentState == Boss.BossState.BS_READY && m_WolvesPool.Count == 0)
                {
                    boss.PlayShow();
                }
                
                boss.update(dt);
            }

            //
            for (int i = 0; i < m_BulletsPool.Count; ++i)
            {
                for (int k = 0; k < m_WolvesPool.Count; ++k)
                {
                    Bullet bullet = m_BulletsPool[i];
                    Wolf wolf = m_WolvesPool[k];
                    if(IsBulletHitWolf(ref bullet,ref wolf))
                    {
                        //
                        if (wolf.GetBalloon().BalloonsNum <= 0)
                        {
                            BunnyPlayer.KillingNum = BunnyPlayer.KillingNum + 1;
                            BunnyPlayer.CurrentScore = BunnyPlayer.CurrentScore + wolf.MyScore;
                            //这里应该播放死亡动画，或者什么的
                            wolf.PlayFallAnimation();
                        }
                        //
                        if (bullet.Power <= 0)
                        {
                            bullet.PlayExplodeAnimationThenRemove(wolf.position);
                            break;
                        }
                    }
                }

                for (int k = 0; k < m_BossesPool.Count; ++k)
                {
                    Bullet bullet = m_BulletsPool[i];

                    if (bullet.Power <= 0)
                    {
                        continue;
                    }
                    
                    Boss boss = m_BossesPool[k];
                    if (IsBulletHitBoss(ref bullet, ref boss))
                    {
                        bullet.Power = 0;
                        bullet.PlayExplodeAnimationThenRemove(boss.position);
                    }
                }
            }
        }

        public void EnemyCollideMainPlayer(BunnyPlayer player)
        {
            for (int i = 0; i < m_EnemyBulletPool.Count; ++i)
            {
                //判断和主角的碰撞
                EnemyBullet bullet = m_EnemyBulletPool[i];
                if (bullet.Power <= 0)
                {
                    continue;
                }
                if (bullet != null && bullet.CurrentState != EnemyBullet.BulletState.BS_DYING)
                {
                    CollideCircle[] PtArray = bullet.CollidePt;
                    int hitCounter = 0;
                    foreach (CollideCircle c in PtArray)
                    {
                        if (player.Intersects(c, true))
                        {
                            hitCounter++;
                            //
                            player.HitPlayer();
                            //
                            bullet.Power = bullet.Power - 1;
                            //
                            if (bullet.Power < 0) return;
                            //
                        }
                    }
                }
            }
        }

        public void ClearAll()
        {
            m_BulletsPool.Clear();
            m_WolvesPool.Clear();
            m_BossesPool.Clear();
            m_EnemyBulletPool.Clear();
        }

        private bool IsObjInScene(DynamicsObj obj)
        {
            //
            CCSize winSize = CCDirector.sharedDirector().getWinSize();
            Rectangle rectSize = new Rectangle(0, 0, (int)winSize.width, (int)winSize.height);
            return rectSize.Contains((int)obj.position.x,(int)obj.position.y);
        }

        private bool IsObjToBottom(DynamicsObj obj,float bY)
        {
            return (obj.position.y <= bY);
        }

        private bool IsBulletHitWolf(ref Bullet bullet,ref Wolf wolf)
        {
            if (bullet != null && bullet.CurrentState != Bullet.BulletState.BS_DYING
                && wolf != null && wolf.CurrentState != Wolf.WolfState.WS_DYING
                && wolf.GetBalloon().State == Balloon.BalloonState.BalloonState_Normal)
            {
                CollideCircle[] PtArray = bullet.CollidePt;
                int hitCounter = 0;
                foreach (CollideCircle c in PtArray)
                {
                    if (wolf.GetBalloon().Intersects(c,true))
                    {
                        hitCounter++;
                        //
                        wolf.m_Balloons.ChangeBalloonNum(wolf.m_Balloons.BalloonsNum - 1);
                        //
                        bullet.Power = bullet.Power - 1;
                        //
                        if (bullet.Power < 0) return true;
                        //
                    }
                    else if (wolf.GetShield()!=null && wolf.GetShield().Intersects(c, true))
                    {
                        bullet.Power = 0;
                        bullet.PlayExplodeAnimationThenRemove(wolf.position);
                        return false;
                    }
                }
                //
                return (hitCounter > 0);
            }
            return false;
        }

        private bool IsBulletHitBoss(ref Bullet bullet, ref Boss boss)
        {
            if (bullet != null && bullet.CurrentState != Bullet.BulletState.BS_DYING
                && boss != null && boss.CurrentState != Boss.BossState.BS_DYING)
            {
                CollideCircle[] PtArray = bullet.CollidePt;
                int hitCounter = 0;
                foreach (CollideCircle c in PtArray)
                {
                    if (boss.Intersects(c, true))
                    {
                        hitCounter++;
                        //
                        boss.HitBoss();
                        //
                        bullet.Power = bullet.Power - 1;
                        //
                        if (bullet.Power < 0) return true;
                        //
                    }
                }
                //
                return (hitCounter > 0);
            }
            return false;
        }
        //
        public void AddChild(DynamicsObj obj)
        {
            Bullet bullet = obj as Bullet;
            if (bullet != null)
            {
                bullet = (Bullet)obj;
                m_BulletsPool.Add(bullet);
                ResDef.g_BulletBatchNode.addChild(bullet);
                return;
            }
            //
            Wolf wolf = obj as Wolf;
            if (wolf != null)
            {
                wolf = (Wolf)obj;
                ResDef.g_WolfBatchNode.addChild(wolf);
                m_WolvesPool.Add(wolf);
                return;
            }

            Boss boss = obj as Boss;
            if (boss != null)
            {
                boss = (Boss)obj;
                ResDef.g_UFOBatchNode.addChild(boss);
                m_BossesPool.Add(boss);
                return;
            }

            EnemyBullet eb = obj as EnemyBullet;
            if (eb != null)
            {
                eb = (EnemyBullet)obj;
                ResDef.g_BulletBatchNode.addChild(eb);
                m_EnemyBulletPool.Add(eb);
                return;
            }
        }
        //
        public void DelChild(DynamicsObj obj)
        {
            Bullet bullet = obj as Bullet;
            if (bullet != null)
            {
                bullet = (Bullet)obj;
                m_BulletsPool.Remove(bullet);
                ResDef.g_BulletBatchNode.removeChild(bullet, false);         
                return;
            }
            //
            Wolf wolf = obj as Wolf;
            if (wolf != null)
            {
                wolf = (Wolf)obj;
                m_WolvesPool.Remove(wolf);
                ResDef.g_WolfBatchNode.removeChild(wolf, true);
                return;
            }

            Boss boss = obj as Boss;
            if (boss != null)
            {
                boss = (Boss)obj;
                m_BossesPool.Remove(boss);
                ResDef.g_UFOBatchNode.removeChild(boss, true);
                return;
            }

            EnemyBullet eb = obj as EnemyBullet;
            if (eb != null)
            {
                eb = (EnemyBullet)obj;
                m_EnemyBulletPool.Remove(eb);
                ResDef.g_BulletBatchNode.removeChild(eb, true);
                return;
            }
        }
 
    }
}
